package com.stx.hotkey.common.coder;

import com.stx.hotkey.common.model.HotKeyMsg;
import com.stx.hotkey.common.tool.ProtostuffUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class MsgDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        try {
            byte[] body = new byte[byteBuf.readableBytes()]; // 传输正常
            byteBuf.readBytes(body);

            list.add(ProtostuffUtils.deserialize(body, HotKeyMsg.class));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
